﻿using System.Collections.Concurrent;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper.SessionStore;

namespace Tin.Esay.Framework.Common.IBatisNet
{
    public static class DataSource
    {
        private static readonly ConcurrentDictionary<string, ISqlMapper> _mappers;

        static DataSource()
        {
            _mappers = new ConcurrentDictionary<string, ISqlMapper>();
        }

        public static ISqlMapper GetMapper(string configPath)
        {
            return _mappers.GetOrAdd(configPath, key => GetOrAddMapper(configPath));
        }

        private static ISqlMapper GetOrAddMapper(string configPath)
        {
            var sqlMapper = new DomSqlMapBuilder().Configure(configPath);

            sqlMapper.SessionStore = new HybridWebThreadSessionStore(sqlMapper.Id);

            return sqlMapper;
        }
    }
}
